package com.daoliuhe.scaffold.web.controller.admin;

import com.daoliuhe.scaffold.model.UserGroup;
import com.daoliuhe.scaffold.model.UserGroupRole;
import com.daoliuhe.scaffold.model.UserGroupUser;
import com.daoliuhe.scaffold.service.UserGroupRoleService;
import com.daoliuhe.scaffold.service.UserGroupService;
import com.daoliuhe.scaffold.service.UserGroupUserService;
import com.daoliuhe.scaffold.web.dto.UserDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

@Controller
@RequestMapping("/userGroup")
public class UserGroupController {
	
	private static final Logger logger = LoggerFactory.getLogger(UserGroupController.class);

	@Autowired
	UserGroupService userGroupService;

	@Autowired
	UserGroupUserService userGroupUserService;

	@Autowired
	UserGroupRoleService userGroupRoleService;

	/**
	 * 获取用户组数据
	 * @return
	 */
	@RequestMapping("/data")
	@ResponseBody
	public Object data(UserGroup userGroup) {
		return userGroupService.getPageData(userGroup);
	}


	/**
	 * 用户组管理页面
	 * @return
	 */

	@RequestMapping("/list")
	public ModelAndView list(UserGroup userGroup) throws UnsupportedEncodingException {
		logger.info("list,userGroup:{}",userGroup);
		ModelAndView mav = new ModelAndView("admin/userGroup/list");
		mav.addAllObjects(userGroupService.getPageData(userGroup));
		mav.addObject("entity", userGroup);
		return mav;
	}

	/**
	 * 用户组管理编辑页面
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit(UserGroup userGroup, @RequestParam(required=false) String tab, UserDto userDto) {
		logger.info("edit,userGroup:{},tab:{},userDto{}",userGroup, tab, userDto);
		ModelAndView mav = new ModelAndView("admin/userGroup/edit");
		if (!StringUtils.isEmpty(userGroup.getId())) {
			UserGroup u = userGroupService.getUserGroupById(userGroup.getId());
			u.setPage(userGroup.getPage());
			u.setRows(userGroup.getRows());
			mav.addObject("entity", u);
			mav.addObject("userDto", userDto);

			// 关联的用户分页
			UserGroupUser ugu=new UserGroupUser();
			ugu.setUserGroupId(userGroup.getId());
			ugu.setPage(userDto.getuPage());
			ugu.setRows(userDto.getuRows());
			mav.addObject("userMap", userGroupUserService.getUserPageData(ugu));
			userDto.setuPage(ugu.getPage());

			// 管理的角色关系分页
			UserGroupRole ugr = new UserGroupRole();
			ugr.setUserGroupId(userGroup.getId());
			ugr.setPage(userDto.getrPage());
			ugr.setRows(userDto.getrRows());
			mav.addObject("roleMap", userGroupRoleService.getRolePageData(ugr));
			userDto.setrPage(ugr.getPage());

			mav.addObject("tab", tab);
		}
		mav.addObject("param", userGroup);
		return mav;
	}

	/**
	 * 新增用户组时的基本校验
	 * @return
	 */
	@RequestMapping("/vName")
	@ResponseBody
	public Object vName(String name, String id) {
		logger.info("vName,name:{},id:{}",name,id);
		return userGroupService.isRightName(name, id);
	}

	/**
	 * 新增或修改用户组
	 * @return
	 */
	@RequestMapping("/saveOrUpdate")
	public String saveOrUpdate(UserGroup userGroup) {
		logger.info("saveOrUpdate,userGroup:{}",userGroup);
		userGroupService.saveOrUpdate(userGroup);
		return "redirect:/userGroup/list";
	}

	/**
	 * 用户组增加用户
	 * @return
	 */
	@RequestMapping("/saveBatchUserForUserGroup")
	@ResponseBody
	public Object saveBatchUserForUserGroup(String userIds, String userGroupId) {
		logger.info("saveBatchUserForUserGroup,userIds:{},userGroupId:{}",userIds,userGroupId);
		return userGroupUserService.saveBatchUserForUserGroup(userIds, userGroupId);
	}

	/**
	 * 用户增加用户组
	 * @return
	 */
	@RequestMapping("/batchAddUserGroup")
	@ResponseBody
	public Object batchAddUserGroup(String userId, String userGroupIds) {
		logger.info("saveBatchUserForUserGroup,userId:{},userGroupIds:{}",userId,userGroupIds);
		return userGroupUserService.doBatchAddUserGroup(userId, userGroupIds);
	}

	/**
	 * 删除用户组用户
	 * @return
	 */
	@RequestMapping("/deleteUser")
	@ResponseBody
	public Object deleteUsers(String userIds, String userGroupId) {
		logger.info("deleteUser,userIds:{},userGroupId:{}",userIds,userGroupId);
		return userGroupUserService.deleteUser(userIds, userGroupId);
	}

	/**
	 * 增加角色
	 * @return
	 */
	@RequestMapping("/batchAddRole")
	@ResponseBody
	public Object batchAddRole(String userGroupId, String roleIds) {
		logger.info("saveBatchUserForUserGroup,userGroupId:{},roleIds:{}",userGroupId,roleIds);
		return userGroupRoleService.saveBatchRole(userGroupId, roleIds);
	}

	/**
	 * 删除用户组角色
	 * @return
	 */
	@RequestMapping("/batchDeleteRole")
	@ResponseBody
	public Object deleteRoles(String userGroupId, String roleIds) {
		logger.info("deleteUser,roleIds:{},userGroupId:{}",roleIds, userGroupId);
		return userGroupRoleService.deleteForUserGroup(userGroupId, roleIds);
	}

	/**
	 * 用户组批量启用
	 * @return
	 */
	@RequestMapping("/updateBatchEnable")
	public String updateBatchEnable(String groupIds) {
		logger.info("updateBatchEnable,groupIds:{}",groupIds);
		String[] idArray = groupIds.split(";");
		ArrayList<String> ids = new ArrayList<String>();
		for (String id : idArray) {
			ids.add(id);
		}
		userGroupService.updateBatchEnable(ids);
		return "redirect:/userGroup/list";
	}

	/**
	 * 用户组批量禁用
	 * @return
	 */
	@RequestMapping("/updateBatchDisable")
	public String updateBatchDisable(String groupIds) {
		logger.info("updateBatchDisable,groupIds:{}",groupIds);
		String[] idArray = groupIds.split(";");
		ArrayList<String> ids = new ArrayList<String>();
		for (String id : idArray) {
			ids.add(id);
		}
		userGroupService.updateBatchDisable(ids);
		return "redirect:/userGroup/list";
	}

	/**
	 * 给用户批量删除用户组
	 * @return
	 */
	@RequestMapping("/deleteUserGroupForUser")
	@ResponseBody
	public Object deleteUserGroupForUser(String userId, String userGroupIds) {
		logger.info("deleteUser,userIds:{},userGroupId:{}",userId,userGroupIds);
		return userGroupUserService.deleteUserGroupForUser(userId, userGroupIds);
	}

	/**
	 * 根据角色添加用户组
	 * @return
	 */
	@RequestMapping("/addForRole")
	@ResponseBody
	public Object addForRole(String userGroupIds, String roleId) {
		logger.info("addForRole,userGroupIds:{},roleId:{}",userGroupIds,roleId);
		return userGroupRoleService.saveBatchUserGroupForRole(userGroupIds, roleId);
	}

	/**
	 * 根据角色删除用户组
	 * @return
	 */
	@RequestMapping("/deleteForRole")
	@ResponseBody
	public Object deleteForRole(String userGroupIds, String roleId) {
		logger.info("deleteForRole,userGroupIds:{},roleId:{}",userGroupIds, roleId);
		return userGroupRoleService.deleteForRole(userGroupIds, roleId);
	}
}
